package io.babystar.modules.business.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.babystar.common.utils.PageUtils;
import io.babystar.common.utils.R;
import io.babystar.modules.business.entity.PartnerTypeEntity;
import io.babystar.modules.business.entity.TeacherEntity;
import io.babystar.modules.business.service.PartnerTypeService;
import io.babystar.modules.business.service.TeacherService;
import io.babystar.modules.sys.controller.AbstractController;
import io.babystar.modules.sys.entity.SysUserEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;



/**
 * 配合人表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2021-07-21 16:19:53
 */
@RestController
@RequestMapping("business/partnerType")
public class PartnerTypeController extends AbstractController {
    @Autowired
    private PartnerTypeService partnerTypeService;
    @Autowired
    private TeacherService teacherService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:partnerType:list")
    public R list(@RequestParam Map<String, Object> params){
        SysUserEntity user = this.getUser();
        params.put("businessId",user.getBusinessId());
        int type = user.getType();
        if(type==2 || type==3){
            long teacherId = user.getParentTeacherId();
            TeacherEntity teacherEntity = teacherService.getById(teacherId);
            params.put("shopId",teacherEntity.getShopId().toString());
        }

        PageUtils page = partnerTypeService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:partnerType:info")
    public R info(@PathVariable("id") Integer id){
		PartnerTypeEntity partnerType = partnerTypeService.getById(id);

        return R.ok().put("partnerType", partnerType);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:partnerType:save")
    public R save(@RequestBody PartnerTypeEntity partnerType){
        SysUserEntity user = this.getUser();
        partnerType.setBusinessId(user.getBusinessId().intValue());
        //查询是否存在
        List<PartnerTypeEntity> list = partnerTypeService.list(new QueryWrapper<PartnerTypeEntity>()
                .eq("business_id", user.getBusinessId())
                .eq("shop_id", partnerType.getShopId())
                .eq("partner", partnerType.getPartner()));
        if(list.size() > 0){
            partnerType.setId(list.get(0).getId());
            partnerTypeService.updateById(partnerType);
        }else{
            partnerTypeService.save(partnerType);
        }

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:partnerType:update")
    public R update(@RequestBody PartnerTypeEntity partnerType){
		partnerTypeService.updateById(partnerType);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:partnerType:delete")
    public R delete(@RequestBody Integer[] ids){
		partnerTypeService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 根据店铺查询
     */
    @RequestMapping("/queryListByShop")
    public R queryListByShop(){
        SysUserEntity user = this.getUser();
        HashMap<String, Object> map = new HashMap<>();
        map.put("business_id",user.getBusinessId());
        int type = user.getType();
        if(type==2){
            long teacherId = user.getParentTeacherId();
            TeacherEntity teacherEntity = teacherService.getById(teacherId);
            map.put("shop_id",teacherEntity.getShopId().toString());
        }else{
            return R.error("只有店长有权限");
        }
        List<PartnerTypeEntity> list = partnerTypeService.listByMap(map);

        return R.ok().put("list", list);
    }

}
